package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.ProductComment;
import com.clkj.xiuzheng.service.shangcheng.ScCommentService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/ScComment")
@CrossOrigin
@Api(value = "/ScComment", tags = {"Z商城评论管理"})
public class ScCommentController {

    @Autowired
    private ScCommentService scCommentService;

    @ApiOperation(value = "小程序根据商品id获取评论(0 好评 1中评 2差评 3有图 4查询全部)")
    @PostMapping("/getProductByProductId")
    public Result getProductByProductId(@ApiParam(name = "productId", value = "产品ID")@RequestParam Integer productId,
                                        @ApiParam(name = "commentType", value = "评论类型0 好评 1中评 2差评 3有图 4查询全部")@RequestParam Integer commentType,
                                        @RequestParam Integer pageSize,
                                        @RequestParam Integer pageNum) {
        try {
            PageInfo pageInfo = scCommentService.getProductByCategoryId(productId,commentType,pageSize,pageNum);
            return new Result(true, StatusCode.OK,"添加成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @ApiOperation(value = "根据(公司id<非必填>和商品ID<非必填>)获取所有评论列表")
    @PostMapping("/getCommentByCompanyId")
    public Result getCommentByCompanyId(@ApiParam(name = "companyId", value = "公司id")@RequestParam(required = false) Integer companyId,
                                        @ApiParam(name = "productId", value = "产品ID")@RequestParam(required = false) Integer productId,
                                        @RequestParam Integer pageSize,
                                        @RequestParam Integer pageNum) {
        try {
            PageInfo list = scCommentService.getCommentByCompanyId(companyId,productId,pageSize,pageNum);
            return new Result(true, StatusCode.OK,"添加成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation(value = "根据评论id字符串删除评论")
    @PostMapping("/deleteComment")
    public Result deleteComment(@ApiParam(name = "productCommentIds", value = "评论id字符串") @RequestParam String productCommentIds) {
        try {
            scCommentService.deleteComment(productCommentIds);
            return new Result(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }
    @ApiOperation(value = "根据评论id字符串置顶评论")
    @PostMapping("/TopComment")
    public Result TopComment(@ApiParam(name = "productCommentIds", value = "评论id字符串")String productCommentIds,@ApiParam(name = "isTop", value = "1置顶0取消置顶")Integer isTop) {
        try {
            scCommentService.TopComment(productCommentIds,isTop);
            return new Result(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }
    @ApiOperation(value = "商品评论导表")
    @PostMapping("/importComment")
    public Result importComment(HttpServletRequest request) {
        try {
            scCommentService.importComment(request);
            return new Result(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }
    @ApiOperation(value = "小程序添加评论")
    @PostMapping("/addComment")
    public Result addComment(@RequestBody ProductComment productComment) {
        try {
            scCommentService.addComment(productComment);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @ApiOperation(value = "小程序添加评论图片")
    @PostMapping("/addCommentImg")
    public Result addCommentImg(HttpServletRequest request) {
        try {
            String name = scCommentService.addCommentImg(request);
            return new Result(true, StatusCode.OK,"添加成功",name);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

}
